package com.suse.common.config.swagger2;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.base.Predicates;
import com.suse.common.util.net.NetUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Slf4j
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${server.port}")
    private String port;

    @Value("${server.servlet.context-path}")
    private String servletContextPath;

    private static final String title = "在线教育系统接口文档";

    private static final String description = "在线教育系统接口文档";

    private static final String contactName = "辜玉彬";

    private static final String contactEmail = "2404337367@qq.com";

    private static final String version = "1.0";

    @Bean
    public Docket createApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("前台接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(Predicates.not(PathSelectors.regex(".*/admin/.*")))
                .build();
    }

    @Bean
    public Docket createAdminApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("后台接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.regex(".*/admin/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        String contectUrl = "http://" + NetUtil.getIp() + ":" + port + servletContextPath +  "/doc.html";
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .contact(new Contact(contactName,contectUrl,contactEmail))
                .version(version)
                .build();
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info(title);
        if (servletContextPath.endsWith("/")) {
            servletContextPath = servletContextPath.substring(0,servletContextPath.length() - 1);
        }
        String contectUrl = "http://" + NetUtil.getIp() + ":" + port + servletContextPath +  "/doc.html";
        log.info("本地地址 : http://localhost:{}{}/doc.html",port,servletContextPath);
        log.info("网络地址 : {}",contectUrl);
    }

}
